//
//  PhotoListBottomView.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2018/12/28.
//  Copyright © 2018年 郑逸凯. All rights reserved.
//

#import "PhotoListBottomView.h"
#import "PhotoCollectionViewCell.h"
#import "PhotoSectionModel.h"
#import "PhotoModel.h"

#define K_ITEM_WIDTH (SCREEN_WIDTH/15)

@interface PhotoListBottomView ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@end

@implementation PhotoListBottomView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:(CGRect)frame]) {
        [self setupSubviews];
        
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        [self setupSubviews];
    }
    return self;
}
#pragma mark - Public Methods

- (void)reloadData {
    [self.collectionView reloadData];
}

- (void)setCurrentPhotoIndex:(NSInteger)index {
    if (index < self.photoArray.count) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
    }
}

#pragma mark - Responder Actions
#pragma mark - UIScrollViewDelegate

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"sss offset %@",NSStringFromCGPoint(scrollView.contentOffset));
//}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSLog(@"sss offset %@ Velocity %@ ",NSStringFromCGPoint(scrollView.contentOffset),NSStringFromCGPoint(velocity));
    CGFloat offsetX = scrollView.contentOffset.x;
    CGFloat itemWidth = K_ITEM_WIDTH;
    if (offsetX < 0 && offsetX > itemWidth * self.photoArray.count) {
        //如果在边界则直接不处理
        return;
    }
    NSInteger index = (NSInteger)offsetX/itemWidth;
    if (velocity.x > 0 && index < self.photoArray.count ) {
        index++;
    }
    *targetContentOffset = CGPointMake(index * itemWidth, 0);
    if (self.delegate && [self.delegate respondsToSelector:@selector(scrolledToIndex:photoListBottomView:)]) {
        [self.delegate scrolledToIndex:index photoListBottomView:self];
    }
}


#pragma mark - UICollectionViewDelegates
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor clearColor];
        return header;
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footer.backgroundColor = [UIColor clearColor];
        return footer;
    } else {
        return nil;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return (CGSize){(SCREEN_WIDTH - K_ITEM_WIDTH)/2,self.frame.size.height};
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return (CGSize){(SCREEN_WIDTH - K_ITEM_WIDTH)/2,self.frame.size.height};
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.photoArray.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PhotoCollectionViewCell*cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollectionViewCell" forIndexPath:indexPath];
    if (!cell ) {
        NSLog(@"cell为空,创建cell");
        cell = [[PhotoCollectionViewCell alloc] init];
    }
    PhotoModel *model = [self.photoArray objectAtIndex:indexPath.row];
    cell.isNormalStyle = NO;
    cell.selectionMode = NO ;
    cell.photoModel = model;
    cell.isSelected = NO;
    cell.index = indexPath.row;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView setContentOffset:CGPointMake(indexPath.row * K_ITEM_WIDTH, 0) animated:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(scrolledToIndex:photoListBottomView:)]) {
        [self.delegate scrolledToIndex:indexPath.row photoListBottomView:self];
    }
}
#pragma mark - Private Methods
- (void)setupSubviews {
    [self addSubview:self.collectionView];
}

#pragma mark - Setters and Getters
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        CGRect collectionViewFrame = self.frame;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        // 设置UICollectionView为横向滚动
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        // 每一行cell之间的间距
        flowLayout.minimumLineSpacing = 0;
        // 每一列cell之间的间距
        flowLayout.minimumInteritemSpacing = 0;
        // 设置第一个cell和最后一个cell,与父控件之间的间距
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        //    flowLayout.minimumLineSpacing = 1;// 根据需要编写
        //    flowLayout.minimumInteritemSpacing = 1;// 根据需要编写
        CGFloat itemHeight = self.frame.size.height;
        flowLayout.itemSize = CGSizeMake(K_ITEM_WIDTH, itemHeight);// 该行代码就算不写,item也会有默认尺寸
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:flowLayout];
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        [collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"PhotoCollectionViewCell"];
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)setPhotoArray:(NSArray *)photoArray {
    _photoArray = photoArray;
    [self.collectionView reloadData];
}

#pragma mark - Helper

@end
